package com.hairline.controller;


import com.hairline.aspect.UserFootprintLog;
import com.hairline.bean.Order;
import com.hairline.bean.OrderGoods;
import com.hairline.bean.vo.BaseRespVo;
import com.hairline.bean.vo.WxOrderGoodsVo;
import com.hairline.bean.vo.WxOrderInfolVO;
import com.hairline.bean.vo.WxOrderListVo;
import com.hairline.service.WxOrderService;
import org.apache.shiro.crypto.hash.Hash;
import org.omg.PortableInterceptor.INACTIVE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/wx/order")
public class WxOrderController {

    @Autowired
    WxOrderService wxOrderService;

    @RequestMapping("list")
    public BaseRespVo list(Integer showType, Integer page,Integer size){
        //查询所有订单数
        int count = wxOrderService.countOrder();
        //查到所有订单
        List<Order> orderList = wxOrderService.selectOrders(showType,page,size);
        //查询所有订单下的商品
        List<WxOrderListVo> orderListVo = wxOrderService.selectOrderGoods(orderList,showType);
        HashMap<String, Object> map = new HashMap<>();
        map.put("data",orderListVo);
        map.put("count",count);
        map.put("totalPages",Math.ceil(count / size));//向上取整
        return BaseRespVo.ok(map);
    }

    @RequestMapping("detail")
    public BaseRespVo detail(Integer orderId){
        //查orderInfo的同时也把VO封装一下
        WxOrderInfolVO orderInfo = wxOrderService.selectOrderById(orderId);
        //查该orderId对应的商品信息
        List<WxOrderGoodsVo> orderGoods = wxOrderService.selectOrderGoodsByOrderId(orderId);
        HashMap<String, Object> map= new HashMap<>();

        map.put("orderInfo",orderInfo);
        map.put("orderGoods",orderGoods);
        return BaseRespVo.ok(map);
    }

    @RequestMapping("cancel")
    public BaseRespVo cancel(@RequestBody HashMap<String,Integer> map){
        boolean b = wxOrderService.updateOrderStatusById(map.get("orderId"),102);
        if (b){
            return BaseRespVo.ok();
        }
        return BaseRespVo.fail();
    }

    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody HashMap<String,Integer> map){
        boolean b = wxOrderService.deleteOrderById(map.get("orderId"));
        if (b){
            return BaseRespVo.ok();
        }
        return BaseRespVo.fail();
    }

    @RequestMapping("refund")
    public BaseRespVo refund(@RequestBody HashMap<String,Integer> map){
        boolean orderId = wxOrderService.updateOrderStatusById(map.get("orderId"), 202);
        if (orderId){
            return BaseRespVo.ok();
        }
        return BaseRespVo.fail();
    }


    @RequestMapping("confirm")
    public BaseRespVo confirm(@RequestBody HashMap<String,Integer> map){
        boolean orderId = wxOrderService.updateOrderStatusById(map.get("orderId"), 401);
        if (orderId){
            return BaseRespVo.ok();
        }
        return BaseRespVo.fail();
    }

    @RequestMapping("goods")
    public BaseRespVo commentGoods(Integer orderId,Integer goodsId){
        List<WxOrderGoodsVo> goods = wxOrderService.selectOrderGoodsByOrderIdAndGoodsId(orderId,goodsId);
        return BaseRespVo.ok(goods.get(0));
    }

    @RequestMapping("comment")
    public BaseRespVo comment(@RequestBody HashMap<String,Object> map){
        boolean b = wxOrderService.commentOrderGoodsById(map);
        if (b){
            return BaseRespVo.ok();
        }
        return BaseRespVo.fail();
    }




}
